package za.org.jcicada.katcp;

/**
 * SensorInteger.java
 *
 * @author Simon Cross
 *
 * Created on October 27, 2008, 17:37
 */

import za.org.jcicada.katcp.ISensor;
import za.org.jcicada.katcp.ISensorValue;
import za.org.jcicada.katcp.SensorBase;
import za.org.jcicada.katcp.SensorValueBase;

/**
 * A base class for KAT device integer sensors.
 */
public class SensorInteger extends SensorBase {

    /**
     * Value class for this sensor.
     */
    static public class IntegerValue extends SensorValueBase {

        /**
         * This Integer sensor's value.
         */
        protected int intValue;

        /**
         * Construct a sensor value with the timestamp set to now, status
         * unknown and value 0.
         */
        public IntegerValue() {
            super();
            intValue = 0;
        }

        /**
         * Construct a sensor value with the given timestamp, status and value.
         *
         * @param timestamp  When the sensor value was recorded.
         * @param status     The sensor status when the value was recorded.
         * @param value      The sensor value.
         */
        public IntegerValue(long timestamp, ISensorValue.Status status, int value) {
            super(timestamp, status);
            intValue = value;
        }

        /**
         * Return the value part of the sensor value.
         *
         * @return The value part of the sensor value.
         */
        public int getValue() {
            return intValue;
        }

    }

    /**
     * The minimum sensor value.
     */
    protected int min;

    /**
     * The maximum sensor value.
     */
    protected int max;

    /**
     * Constructs a SensorInteger object.
     *
     * @param name         The sensor name.
     * @param description  A description of the sensor.
     * @param units        The units of the sensor value.
     * @param min          The maximum value this sensor can attain.
     * @param max          The minimum value this sensor can attain.
     */
    public SensorInteger(String name, String description, String units,
                         int min, int max) {
        super(name, ISensor.Type.INTEGER, new IntegerValue(),
              description, units,
              new String[] { String.valueOf(min), String.valueOf(max) }); 
        this.min = min;
        this.max = max;
    }

    /**
     * Format a sensor value of the same type as this ISensorType object.
     *
     * @param value  Sensor value to format.
     * @return  The value formatted as a string.
     */
    public String formatValue(ISensorValue value) {
        return String.valueOf(((IntegerValue) value).getValue());
    }

}
